﻿// var authority = [1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; //用户权限列表
//0: 查看项目 1: 管理项目 2: 查看委托单和委托人 3: 查看、新建（自己的）委托单 4: 新建、查看委托单和委托人
//5: 查看报告 6: 发布报告 7: 制定报告 8: 审核报告 9: 查看通知单 10: 发布通知单（评片） 11: 新建、匹配工艺卡
//12: 审核工艺卡 13: 分配检测小组 14: 分配检测人员 15: 查看派工单 16: 管理检测小组 17: 填写操作记录
//18: 分配权限 19: 检测部门管理


var rawData = [[], [], []];
var sentMessage;
var postData;
//获得派工单信息
if (authority[18] === 1) {   //超管查看派工单
    sentMessage = {
        "requestType": "superManager",
    }
} else {
    if (authority[14] === 1) {
        if (authority[16] === 1){   //主管
            sentMessage = {
                "requestType": "manager",
                "user_part": user.staff_part
            }
        }else { //组长
            sentMessage = {
                "requestType": "leader",
                "user_id": user.user_id
            }
        }
    } else if (authority[15] === 1) { //检测人员查看派工单
        sentMessage = {
            "requestType": "worker",
            "user_id": user.user_id
        }
    } else {
        alert("你没有权限查看此内容");
        window.location.href = "index.html";
    }
}
if (authority[14] === 1 || authority[18] === 1) {
    // sentMessage = {
    //     "requestType": "leader",
    //     "user_id": user.user_id
    // };
    console.log(sentMessage)
    $.ajax({
        url: '/op/taskpaper_list',
        type: 'post',
        data: sentMessage,
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                postData = data;
                console.log('未分配数据：', postData);
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    for (let i = 0; i < postData.length; i++) {
        var date = new Date(postData[i].tp_start);
        postData[i].tp_start = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
        date.getDate();
        rawData[0].push([
            postData[i].tp_id,
            postData[i].task_pname,
            postData[i].task_type,
            postData[i].wc_num,
            postData[i].tp_start == null ? "" : postData[i].tp_start,
            postData[i].tech_id == null ? "无" : "有",
            postData[i].tp_wcid
        ]);
    }
}
if (authority[15] === 1) {
    // sentMessage = {
    //     "requestType": "worker",
    //     "user_id": user.user_id
    // };
    $.ajax({
        url: '/op/dispatch_list',
        type: 'post',
        data: sentMessage,
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                postData = data;
                console.log(postData);
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
}
//装填数据
if (authority[14] === 1 || authority[18] === 1) {
    console.log(sentMessage)
    $.ajax({
        url: '/op/dispatch_list',
        type: 'post',
        // data: {
        //     "requestType": "leader",
        //     "user_uid": user.user_id
        // },
        data: sentMessage,
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                postData = data;
                console.log('已分配数据：', postData)
                // window.location.href = '/projectManage.html';
            }

        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    for (let i = 0; i < postData.length; i++) {
        var date = new Date(postData[i].d_start);
        postData[i].d_start = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
        // console.log('测试日期格式');
        //console.log(postData[i].d_start);
        date.getDate();
        if (postData[i].d_fin != null) {
            var date2 = new Date(postData[i].d_fin);
            postData[i].d_fin = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
            date2.getDate();
        }
        rawData[1].push([
            postData[i].d_id,
            postData[i].task_pname,
            postData[i].task_type,
            postData[i].d_start,
            postData[i].tech_id == null ? "无" : "有",
            postData[i].d_fin,
            postData[i].user_name,
            postData[i].d_wcid
        ]);
    }
}
if (authority[15] === 1) {
    for (let i = 0; i < postData.length; i++) {
        var date = new Date(postData[i].d_start);
        postData[i].d_start = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
        date.getDate();
        if (postData[i].d_fin != null) {
            var date2 = new Date(postData[i].d_fin);
            postData[i].d_fin = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
            date2.getDate();
        }
        let temp = [
            postData[i].d_id,
            postData[i].task_pname,
            postData[i].task_type,
            postData[i].d_start == null ? "" : postData[i].d_start,
            postData[i].tech_id == null ? "无" : "有",
            postData[i].d_fin == null ? "-" : postData[i].d_fin,
            postData[i].d_tid,
            postData[i].d_wcid,
            postData[i].d_tpid
        ]
        rawData[0].push(temp);
        if (postData[i].d_fin == null) {
            rawData[1].push(temp);
        } else {
            rawData[2].push(temp);
        }
    }
}
var dummyData = [].concat(rawData);

var title;
var methodCount;
var neededOptionIndex;
//显示选项卡
var optionCard = document.getElementById("optionCard");
//初始化表格标题，根据权限类型动态展示新建按钮和选项卡
if (authority[14] === 1) {//检测组长页面
    optionCard.innerHTML =
        "<li class=\"active\"><a href=\"#lA\" data-toggle=\"tab\">未分配</a></li>" +
        "<li><a href=\"#lB\" data-toggle=\"tab\">已分配</a></li>";

    document.getElementById("lC").setAttribute("class", "tab-pane");
    document.getElementById("lA").setAttribute("class", "tab-pane active");
    title = [
        ["编号", "项目名称", "委托单类型", "工件数量", "派工日期", "是否有工艺卡", "派工单", "操作"],//“未分配”
        ["编号", "项目名称", "委托单类型", "派工日期", "是否有工艺卡", "完成日期", "检测人员", "派工单", "操作"]//“已分配”
    ];
    methodCount = [6, 7];//除去下拉框的元素数目
    neededOptionIndex = [
        [1, 2, 3, 5],
        [1, 2, 4, 6]
    ];
}
else if (authority[15] === 1) {//检测人员页面
    optionCard.innerHTML =
        "<li class=\"active\"><a href=\"#lA\" data-toggle=\"tab\">全部</a></li>" +
        "<li><a href=\"#lB\" data-toggle=\"tab\">未处理</a></li>" +
        "<li><a href=\"#lC\" data-toggle=\"tab\">已处理</a></li>";

    document.getElementById("lC").setAttribute("class", "tab-pane");
    document.getElementById("lA").setAttribute("class", "tab-pane active");
    title = [
        ["编号", "项目名称", "委托单类型", "派工日期", "是否有工艺卡", "完成日期", "派工单", "操作记录"],//“全部”
        ["编号", "项目名称", "委托单类型", "派工日期", "是否有工艺卡", "完成日期", "派工单", "操作记录"],//“未处理”
        ["编号", "项目名称", "委托单类型", "派工日期", "是否有工艺卡", "完成日期", "派工单", "操作记录"]//“已处理”
    ];
    methodCount = [6, 6, 6];//除去下拉框的元素数目
    neededOptionIndex = [
        [1, 2, 4],
        [1, 2, 4],
        [1, 2, 4]
    ];
}

var pageID = [1, 1, 1];
//设置每一页的行数
var countPerPage = [];

//默认情况下
if (user.countPerPage == 0) {
    countPerPage = [10, 10, 10];
    $('.rowNum').val(10)
} else {
    countPerPage = [user.countPerPage, user.countPerPage, user.countPerPage];
    $('.rowNum').val(user.countPerPage)
}

//点击设置每一页行数的确定按钮
$('.rowsBut').click(function () {
    //获得文本框中输入的值
    var rowsNumbuer = $('.rowNum').val();
    //将该值传入到后台中
    $.ajax({
        url: '/pj/countPerPage',
        type: 'post',
        data: {'rowsNumbuer': rowsNumbuer},
        async: false,
        success: function () {
            //console.log('点击确认以后的countPerPage'+user.userId)
            if (user.countPerPage == 0) {
                countPerPage = [10, 10, 10];
                $('.rowNum').val(10)
            } else {
                countPerPage = [user.countPerPage, user.countPerPage, user.countPerPage];
                $('.rowNum').val(user.countPerPage)
            }
        },
        error: function () {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    //刷新页面
    location.reload()
})

var pageButtonCount = [8, 8, 8];

var pageCount;

pageCount = new Array();
for (let i = 0; i < methodCount.length; i++) {
    pageCount[i] = parseInt((dummyData[i].length - 1) / countPerPage[1]) + 1;
}

var table = [document.getElementById("table0"), document.getElementById("table1"), document.getElementById("table2")];
var tbody = [table[0].children[1], table[1].children[1], table[2].children[1]];
var pages = [document.getElementById("pages0"), document.getElementById("pages1"), document.getElementById("pages2")];
// console.log(tbody[0].innerHTML);
//初始化筛选栏可选项
for (let i = 0; i < neededOptionIndex.length; i++) {
    for (let j = 0; j < neededOptionIndex[i].length; j++) {
        let option = document.createElement("option");
        option.innerHTML = title[i][neededOptionIndex[i][j]];
        document.getElementById("searcher" + i).children[1].appendChild(option);
    }
}
//for (let i = 0; i < methodCount.length; i++) {
//    for (let j = 0; j < methodCount[i]; j++) {
//        let option = document.createElement("option");
//        option.innerHTML = title[i][j];
//        document.getElementById("searcher" + i).children[1].appendChild(option);
//    }
//}
//初始化表格
for (let i = 0; i < methodCount.length; i++) {
    let inner = "<td>&nbsp;</td>";
    for (let j = 0; j < title[i].length - 1; j++) {
        inner += "<td />";
    }
    for (let j = 0; j < countPerPage[i]; j++) {
        var tr = document.createElement("tr");
        tr.innerHTML = inner;
        tbody[i].appendChild(tr);
    }
}
//装填标题栏
for (let i = 0; i < methodCount.length; i++) {
    for (let j = 0; j < (pageCount[i] > pageButtonCount[i] ? pageButtonCount[i] : pageCount[i]); j++) {
        var li = document.createElement("li");
        li.setAttribute("onclick", "OnClick(" + (j + 1) + ", " + i + ")");
        var a = document.createElement("a");
        a.innerHTML = j + 1;
        a.setAttribute("href", "#");
        li.appendChild(a);
        pages[i].insertBefore(li, pages[i].lastElementChild);
    }
    for (let j = 0; j < title[i].length; j++) {
        let t = document.createElement("th");
        t.innerHTML = title[i][j];
        table[i].children[0].children[0].appendChild(t);
    }
    OnClick(1, i);
}

// 生成翻页按钮
function OnClick(index, cardIndex) {
    if (index === -1) {
        if (pageID[cardIndex] > 1) OnClick(pageID[cardIndex] - 1, cardIndex);
        return;
    }
    if (index === -2) {
        if (pageID[cardIndex] < pageCount[cardIndex]) OnClick(pageID[cardIndex] + 1, cardIndex);
        return;
    }
    if (index === 1 && dummyData[cardIndex].length < countPerPage[cardIndex]) {
        for (let i = 0; i < dummyData[cardIndex].length; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        for (let i = dummyData[cardIndex].length; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "display: none");
        }
        pages[cardIndex].setAttribute("style", "display: none");
    }
    else {
        for (let i = 0; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        pages[cardIndex].setAttribute("style", "");
    }
    pageID[cardIndex] = index;
    //显示数据
    for (let i = 0; i < countPerPage[cardIndex]; i++) {
        if (dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i] !== undefined) {
            for (let j = 0; j < methodCount[cardIndex]; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][j];
            }
            //显示操作按钮
            if (authority[14] === 1) {
                //派工单分配——检测组长页面
                if (cardIndex === 0) {//“未分配”
                    let button = document.createElement("a");
                    button.setAttribute("href", "javascript:void(0)");
                    button.setAttribute("onclick", "window.location.href='/viewWorkOrder.html?dispatched=false&wcid="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][6] + "'");
                    button.innerHTML = "查看";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 2].innerHTML = button.outerHTML;//倒数第一个

                    button = document.createElement("a");
                    button.setAttribute("href", "javascript:void(0)");
                    button.setAttribute("onclick", "window.location.href='/allocateWorkpiece.html?wcid="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][6] + "&tp_id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");
                    button.innerHTML = "分配";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;//左数第二个
                }
                else if (cardIndex === 1) {// “已分配”
                    let button = document.createElement("a");
                    button.setAttribute("href", "javascript:void(0)");
                    button.setAttribute("onclick", "window.location.href='/viewWorkOrder.html?dispatched=true&wcid="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][7] + "&d_id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");
                    button.innerHTML = "查看";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 2].innerHTML = button.outerHTML;//倒数第二个

                    button = document.createElement("a");
                    button.setAttribute("href", "javascript:void(0)");
                    button.setAttribute("onclick", "window.location.href='/viewDepute.html?id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");
                    button.innerHTML = "催单";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;//倒数第一个
                }
            }
            else if (authority[15] === 1) {//派工单管理、查看——检测人员页面
                // "全部"、“未处理”、“已处理” 倒数第二个全部变成“查看”
                let button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                button.setAttribute("onclick", "window.location.href='/viewWorkOrder.html?dispatched=true&wcid="
                    + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][7] + "&d_id="
                    + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");
                button.innerHTML = "查看";
                tbody[cardIndex].children[i].children[title[cardIndex].length - 2].innerHTML = button.outerHTML;

                // "全部"、“未处理”、“已处理” 倒数第一个部分变成“去添加”，
                //“去添加”和“查看”功能各归一类
                button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                if (tbody[cardIndex].children[i].children[title[cardIndex].length - 3].innerHTML === "-") { //看倒数第三个是不是“未处理”
                    button.setAttribute("onclick", "window.location.href='/createOperation.html?d_tid=" +
                        dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][6] + "&d_wcid=" +
                        dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][7] + "&w_did=" +
                        dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "&d_tpid=" +
                        dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][8] + "'");
                    button.innerHTML = "创建";
                }
                else {
                    button.setAttribute("onclick", "window.location.href='/viewOperation.html?d_tid=" +
                        dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][6] + "&d_wcid=" +
                        dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][7] + "&w_did=" +
                        dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "&d_tpid=" +
                        dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][8] + "'");
                    button.innerHTML = "查看";
                }
                tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;// 改最后一列按钮
            }
        } else {
            tbody[cardIndex].children[i].children[0].innerHTML = "&nbsp;";
            for (let j = 1; j < title[cardIndex].length; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = "";
            }
        }
    }
    //刷新翻页按钮
    if (pageCount[cardIndex] > pageButtonCount[cardIndex]) {
        var first = parseInt(pageID[cardIndex] - pageButtonCount[cardIndex] / 2) + 1;
        if (first < 1) first = 1;
        if (first > pageCount[cardIndex] - pageButtonCount[cardIndex] + 1) first = pageCount[cardIndex] - pageButtonCount[cardIndex] + 1;
        for (let i = 0; i < pageButtonCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = first; i < first + pageButtonCount[cardIndex]; i++) {
            var li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + i + ", " + cardIndex + ")");
            if (i === pageID[cardIndex])
                li.setAttribute("class", "active");
            var a = document.createElement("a");
            a.innerHTML = i;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    } else {
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + (i + 1) + ", " + cardIndex + ")");
            if (i + 1 === pageID[cardIndex])
                li.setAttribute("class", "active");
            a = document.createElement("a");
            a.innerHTML = i + 1;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    }
    // console.log("d" + pageID);
}

function OnConfirmDepute() {
    window.location.href = "newDepute.html?id=" + projectName[projectOption.selectedIndex][1];
}

function OnChange(select, index) {//这里还没做好
    switch (select.selectedIndex) {
        case 1: {
            window.location.href = "/viewWorkOrder.html?id=" + index;//派工单
            break;
        }
        case 2: {
            window.location.href = "/viewDepute.html?id=" + index;//委托单描述
            break;
        }
        case 3: {
            window.location.href = "/viewTecCard.html?id=" + index;//工艺卡编号
            break;
        }
    }
}

var groupInfo = [["Group1", 1001], ["Group2", 1002]];
var deputeIndex;

function OnChooseGroup(index) {
    deputeIndex = index;
    let inner = "";
    for (let i = 0; i < groupInfo.length; i++) {
        inner += "<option>" + groupInfo[i][0] + "</option>";
    }
    document.getElementById("groupOption").innerHTML = inner;
    document.getElementById("chooseGroup").setAttribute("class", "modal fade in");
    document.getElementById("chooseGroup").setAttribute("style", "display: block; padding-right: 17px;");
}

function OnCloseGroup() {
    document.getElementById("chooseGroup").setAttribute("class", "modal fade");
    document.getElementById("chooseGroup").setAttribute("style", "display: none;");
}

function OnConfirmGroup() {
    let index = document.getElementById("groupOption").selectedIndex;
    alert("你选择了小组" + groupInfo[index][0] + "(" + groupInfo[index][1] + ")，保存功能未实现OvO");
    document.getElementById("chooseGroup").setAttribute("class", "modal fade");
    document.getElementById("chooseGroup").setAttribute("style", "display: none;");
    location.reload();
}

function OnSearch(cardIndex) {
    let searcher = document.getElementById("searcher" + cardIndex);//搜索栏对象
    let condition = searcher.children[1].selectedIndex;//下拉菜单中选择的那一项的下标
    let details = searcher.children[3].value;//检索的内容
    let timeRange = [searcher.children[5].value, searcher.children[7].value];//两个用来检索的时间，中间必定有一个逗号
    var handyData = rawData[cardIndex];//该页面上的信息
    var resultData = new Array();//存放检索结果
    for (let i = 0; i < handyData.length; i++) {
        //字符串匹配，长度为0则全匹配
        var regx = new RegExp(details);
        //if (regx.exec(handyData[i][condition])) {
        if (regx.exec(handyData[i][neededOptionIndex[cardIndex][condition]])) {
            if (authority[14] === 1) {
                if (cardIndex === 0) {
                    if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][4])
                        continue;
                    if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][4])
                        continue;
                }
                else if (cardIndex === 1) {
                    if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][3])
                        continue;
                    if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][3])
                        continue;
                }
            }
            else if (authority[15] === 1) {
                if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][3])
                    continue;
                if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][3])
                    continue;
            }
            resultData.push(handyData[i]);//加入符合的项            
        }
    }
    dummyData[cardIndex] = resultData;//替换，反正再返回当前页面会变回来

    OnClick(1, cardIndex);//刷新
}